import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class startRemotePeers {

	boolean debug = false;
	public static void main(String[] args) {
		
		startRemotePeers start = new startRemotePeers();
		if (args.length > 0){
			start.debug = true;
		}
		FileReader fr = null;
		try {
			fr = new FileReader("PeerInfo.cfg");
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		BufferedReader br = new BufferedReader(fr);
		String read;
		StringTokenizer readTokenizer;
		String workingDir = System.getProperty("user.dir");
		try {
			int peerID;
			String peerHostName;
			while ((read = br.readLine()) != null) {
				readTokenizer = new StringTokenizer(read);
				peerID = Integer.parseInt(readTokenizer.nextToken());
				peerHostName = readTokenizer.nextToken();
				
				//Runtime.getRuntime().exec(
				//		"ssh " + peerHostName + " cd " + workingDir
				//				+ " ; java BitTorrent" + "  " + peerID);
				start.new HandlePeer(peerHostName, peerID).start();
			}
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	class HandlePeer extends Thread {
		BufferedReader serOut;
		BufferedReader serErr; 
		String host;
		int peerId;
		
		public HandlePeer(String host, int peerId) {
			this.host = host;
			this.peerId = peerId;
		}
		
		public void run(){
			Process shell = startClient(host, "java peerProcess "+peerId);
			readErr(shell);
		}
		
		private Process startClient(String clientIp, String string) {
			Process shell = getSSHShell(clientIp);
			PrintWriter cmdIn = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(shell.getOutputStream())), true);

			serOut = new BufferedReader(new InputStreamReader(
					shell.getInputStream()));
			serErr = new BufferedReader(new InputStreamReader(
					shell.getErrorStream()));

			String path = System.getProperty("user.dir");
			// System.out.println("/bin/sh");
			// cmdIn.println("/bin/sh");
			System.out.println("cd " + path);
			cmdIn.println("cd " + path);
			cmdIn.println(string);
			System.out.println(string);

			// readErr(shell);
			return shell;
		}

		private Process getSSHShell(String clientIp) {
			try {
				Process ssh = Runtime.getRuntime().exec("ssh " + clientIp);
				System.out.println("ssh " + clientIp);
				// Process ssh = Runtime.getRuntime().exec("/bin/bash");
				// System.out.println("/bin/bash");

				// windows
				// Process ssh = Runtime.getRuntime().exec("cmd.exe");
				return ssh;
			} catch (IOException e) {
				System.err.println("Client did not start");
				e.printStackTrace();
				return null;
			}
		}

		private void readErr(Process proc) {
			BufferedReader dataOut = new BufferedReader(new InputStreamReader(
					proc.getInputStream()));
			BufferedReader dataErr = new BufferedReader(new InputStreamReader(
					proc.getErrorStream()));
			try {
				String read;
				while(!(read = dataErr.readLine()).contains("terminate")){
					System.err.println(read);
				}
				System.err.println(read);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		
		private void readOut(Process proc) {
			BufferedReader dataOut = new BufferedReader(new InputStreamReader(
					proc.getInputStream()));
			BufferedReader dataErr = new BufferedReader(new InputStreamReader(
					proc.getErrorStream()));
			try {
				while(true){
					System.out.println(dataOut.readLine());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
